package com.cloudfun.campusshare.repo;

import com.cloudfun.campusshare.common.constant.OrderStatus;
import com.cloudfun.campusshare.entity.OrderEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.Collection;
import java.util.List;
import java.util.Map;

public interface OrderEntityRepo extends
        PagingAndSortingRepository<OrderEntity, String>,
        JpaSpecificationExecutor<OrderEntity>, JpaRepository<OrderEntity, String> {

    @Query("select sum(o.settlementAmount) from OrderEntity o where o.userId=?1 and o.orderStatus=?2")
    Double orderTotal(String userId, OrderStatus orderStatus);

    List<OrderEntity> findAllByUserIdIn(Collection<String> borrowerIds);

    OrderEntity findByOrderNo(String orderNo);

    OrderEntity findByIdAndCustomerId(String id, String customerId);

    List<OrderEntity> findByDeviceIdAndOrderStatus(String deviceId, OrderStatus orderStatus);

    @Query(value = "select sum(price) as journalAmount,sum(discount_price) as discountPrice,sum(handing_fee) as handingFee," +
            "sum(actual_pay_amount) as settlementAmount,user_id as merchantId,pay_platform as payPlatform from device_order " +
            "where FROM_UNIXTIME(CAST(create_time AS SIGNED ) / 1000,'%Y-%m-%d') = ?1 AND order_status!='REFUND' GROUP BY user_id,pay_platform", nativeQuery = true)
    List<Map> balanceAmountPerDay(String date);

    Long countAllByCustomerIdAndOrderStatusAndIsSubscribe(String userId, OrderStatus orderStatus, Boolean isSubscribe);

    List<OrderEntity> findAllByDeviceIdAndOrderStatusIn(String deviceId, Collection<OrderStatus> orderStatus);

    List<OrderEntity> findByCustomerIdAndDeviceIdAndOrderStatus(String customerId, String deviceId, OrderStatus orderStatus);

}
